package com.ruoyi.project.applet.controller;

import java.util.List;

import com.ruoyi.framework.security.LoginUser;
import com.ruoyi.project.system.domain.SysUserWx;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.applet.domain.Prize;
import com.ruoyi.project.applet.service.IPrizeService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 奖品Controller
 * 
 * @author ruoyi
 * @date 2025-06-03
 */
@RestController
@RequestMapping("/applet/prize")
public class PrizeController extends BaseController
{
    @Autowired
    private IPrizeService prizeService;

    /**
     * 查询奖品列表
     */

    @GetMapping("/list")
    public TableDataInfo list(Prize prize)
    {
        startPage();
        List<Prize> list = prizeService.selectPrizeList(prize);
        return getDataTable(list);
    }

    /**
     * 导出奖品列表
     */

    @Log(title = "奖品", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Prize prize)
    {
        List<Prize> list = prizeService.selectPrizeList(prize);
        ExcelUtil<Prize> util = new ExcelUtil<Prize>(Prize.class);
        return util.exportExcel(list, "prize");
    }

    /**
     * 获取奖品详细信息
     */

    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(prizeService.selectPrizeById(id));
    }

    /**
     * 新增奖品
     */

    @Log(title = "奖品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Prize prize)
    {
        return toAjax(prizeService.insertPrize(prize));
    }

    /**
     * 修改奖品
     */

    @Log(title = "奖品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Prize prize)
    {
        return toAjax(prizeService.updatePrize(prize));
    }

    /**
     * 删除奖品
     */

    @Log(title = "奖品", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(prizeService.deletePrizeByIds(ids));
    }

    /**
     * 抽奖
     */
    @GetMapping("/draw")
    public AjaxResult draw() {
        LoginUser loginUser = getLoginUser();
        SysUserWx userWx = loginUser.getUserWx();
        if (userWx == null) {
            return AjaxResult.error("请先登录");
        }
        return prizeService.draw(userWx.getOpenId());
    }

    /**
     * 抽奖点和抽奖记录接口
     */
    @GetMapping("/point")
    public AjaxResult point() {
        LoginUser loginUser = getLoginUser();
        SysUserWx userWx = loginUser.getUserWx();
        if (userWx == null) {
            return AjaxResult.error("请先登录");
        }
        return AjaxResult.success(prizeService.point(userWx.getOpenId()));
    }
}
